<?php

/**
 * 公共操作
 * @author wyb
 */

namespace Logistics\Controller;
use Think\Controller;
class PublicController extends Controller {

    /**
     * 商家登录
     */
    public function login() {
        $this->display();
    }
	
	
	public function login_post() {
		$name = htmlspecialchars( $_POST['name'] );
		$pass = htmlspecialchars( $_POST['pass'] );
		if( !$name || !$pass ) {
			$this->error("用户名或密码不能为空");
		}
		if( !$info = M("logistics_supply")->where(array("username" => $name))->find() ){
			$this->error("用户名或密码错误");
		}
		if( $info['passwd'] != md5( $pass ) ) {
			$this->error("用户名或密码错误");
		}
		
		if( $info['status'] <= 0 ) {
			$this->error("账户已被锁定，请联系客服");
		}
		
		$info['login_time'] = time();
		$info['login_ip'] = get_client_ip();
		$info['login_num'] = $info['login_num'] + 1;
		
		M("logistics_supply")->save( $info ); //更新数据
		
		session( "login_info" , $info ); //存入session
		
		$this->success("登录成功");
	}
	
	public function area() {
		$area = M("area")->field("areaid as id,areaname as name,parent_id")->select();
		$area_data = array();
		foreach( $area as $k => $v ) {
			 $area_data[$v['parent_id']][] = $v;
		}
		$area = $area_data[0];
		foreach( $area as $key => $val ) {
			$area_info = '';
			if( isset( $area_data[$val['id']] ) ) {
				$area_info =  $area_data[$val['id']];
				foreach( $area_info as $k => $v ) {
					$area_level = array();
					if( isset( $area_data[$v['id']] ) ) {
						$area_level = $area_data[$v['id']];
					}
					$area_info[$k]['sub'] = $area_level;
				}
			}
			$area[$key]['sub'] = $area_info;
		}
		$this->success($area);
	}
	
	public function loginout() {
		
		session( "login_info" , null ); //存入session
		
		session_destroy();
		
		$this->success("退出登录成功" , U("public/login"));
	}
}
